package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.common.deploy.R;
import com.example.reggie.common.table.MsgTable;
import com.example.reggie.dto.SetmealDto;
import com.example.reggie.entity.Category;
import com.example.reggie.entity.Setmeal;
import com.example.reggie.service.CategoryService;
import com.example.reggie.service.SetmealDishService;
import com.example.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RequestMapping("/setmeal")
@RestController
public class SetmealController {
    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐
     *
     * @param dto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto dto) {
        setmealService.saveWithDish(dto);
        return R.success(MsgTable.SAVE_OK);
    }

    @GetMapping("/page")
    public R<Page> getPage(int page, int pageSize, String name) {
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> dtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Setmeal::getName, name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo);

        BeanUtils.copyProperties(pageInfo, dtoPage, "records");

        List<SetmealDto> setmeal_dto_records = pageInfo.getRecords().stream().map((item) -> {
            SetmealDto dto = new SetmealDto();
            Category category = categoryService.getById(item.getCategoryId());
            if (category != null) {
                String categoryName = category.getName();
                dto.setCategoryName(categoryName);
            }
            BeanUtils.copyProperties(item, dto);
            return dto;
        }).collect(Collectors.toList());

        dtoPage.setRecords(setmeal_dto_records);

        return R.success(dtoPage);
    }

    /**
     * 删除套餐
     *
     * @param ids
     * @return
     */
    //TODO 稍有点奇怪的传参
    @DeleteMapping
    public R<String> delete(Long[] ids) {
        List<Long> lst = Arrays.asList(ids);
        setmealService.removeWithDish(lst);
        return R.success(MsgTable.DELETE_OK);
    }

    @PostMapping("/status/0")
    public R<String> stopStatus(Long[] ids) {
        Arrays.stream(ids).forEach((id) -> {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(0);
            setmealService.updateById(setmeal);
        });
        return R.success(MsgTable.UPDATE_OK);
    }

    @PostMapping("/status/1")
    public R<String> runStatus(Long[] ids) {
        Arrays.stream(ids).forEach((id) -> {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(1);
            setmealService.updateById(setmeal);
        });
        return R.success(MsgTable.UPDATE_OK);
    }

    @GetMapping("/list")
    public R<List<Setmeal>> getList(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
}
